package appname.example.com.testmvvm;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import appname.example.com.testmvvm.databinding.BviewViewBinding;

public class BacvitivyCtr {


    BviewViewBinding binding;

    private ArrayList<AdapterViewModel> models = new ArrayList<>();
    private Myadapter myadapter;

    public BacvitivyCtr(BviewViewBinding binding) {
        this.binding = binding;
        initData();
        init();
    }

    public void initData() {
        for (int i = 0; i < 30; i++) {
            AdapterViewModel model = new AdapterViewModel();
            model.setName("name");
            model.setAge(String.valueOf(i));
            models.add(model);
        }
    }


    public void init() {
        binding.recycler.setLayoutManager(new LinearLayoutManager(binding.getRoot().getContext()));
        myadapter = new Myadapter();
        binding.recycler.setAdapter(myadapter);

    }


    class Myadapter extends RecyclerView.Adapter<Myadapter.MyHolder> {

        ViewDataBinding binding;


        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()),
                    R.layout.adapter_view, viewGroup, false);
            return new MyHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
            myHolder.binddata(models.get(i));
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder holder, int position, @NonNull List<Object> payloads) {
            super.onBindViewHolder(holder, position, payloads);
        }

        @Override
        public int getItemCount() {
            return models.size();
        }

        class MyHolder extends RecyclerView.ViewHolder {
            ViewDataBinding binding;


            public MyHolder(ViewDataBinding binding) {
                super(binding.getRoot());
                this.binding = binding;
            }

            public void binddata(AdapterViewModel model) {
                binding.setVariable(BR.ctrl, model);

            }
        }
    }

    public void addDataAt(int position, AdapterViewModel model) {
        models.add(position, model);
        myadapter.notifyItemInserted(position);
    }

    public void removeDataAt(int position) {
        models.remove(position);
        myadapter.notifyItemRemoved(position);
    }

    public void  aVoid(){
        myadapter.notifyItemMoved(3,4);
    }

    // onbindviewholder 方法，三个参数的。
    public void  bvoid(){
//        myadapter.notifyItemChanged(3,);
//        myadapter.getd
    }


    public void haha(View view) {

        AdapterViewModel model = new AdapterViewModel();
        model.setAge("66666");
        model.setName("aaaa");
        addDataAt(3,model);
    }

    public void hehe(View view){
        removeDataAt(3);
    }

    public void hehe1(View view){
        aVoid();
    }
}
